<template>
  <a-modal
    :visible="visible"
    title="招生单位维护"
    :width="700"
    @cancel="() => { $emit('close') }"
    :confirmLoading="confirmLoading"
    @ok="handleSave"
  >
    <a-select style="width:205px" v-model="selectedIndex" :filterOption="filterOption" showSearch>
      <a-select-option v-for="(i,index) in collegeList" :key="i.id" :value="index">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
    </a-select>
    <a-button @click="addRecruitUnit" type="primary">添加单位</a-button>
    <a-table
      :columns="columns"
      :dataSource="recruitUnitList"
      size="middle"
      :pagination="false"
      :scroll="{ y: 240 }"
    >
      <template slot="operation" slot-scope="text, record, index">
        <a-popconfirm title="是否要删除此行？" @confirm="remove(index)">
          <a>删除</a>
        </a-popconfirm>
      </template>
    </a-table>
  </a-modal>
</template>

<script>

export default {
  props: ['visible', 'teacherInfo'],
  data () {
    return {
      collegeList: [],
      recruitUnitList: [],
      selectedIndex: null,
      columns: [
        {
          title: '单位编号',
          width: 100,
          dataIndex: 'yxm'
        },
        {
          title: '单位名称',
          width: 350,
          dataIndex: 'dwmc'
        },
        {
          title: '单位类别',
          width: 100,
          dataIndex: 'dwlbmc'
        },
        {
          title: '操作',
          width: 100,
          scopedSlots: { customRender: 'operation' }
        }
      ],
      confirmLoading: false
    }
  },
  watch: {
    teacherInfo (val) {
      if (val.dsbh) {
        this.selectedIndex = null
        this.$api.teacher.teacherRecruitUnit({dsbh: val.dsbh, zsnd: val.zsnd}).then(res => {
          this.recruitUnitList = res.data
        })
      }
    }
  },
  created () {
    this.$api.base.collegeAll().then(res => {
      this.collegeList = res.data
    })
  },
  methods: {
    filterOption (input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    remove (index) {

      let params = {
        dsbh: this.teacherInfo.dsbh,
        zsnd: this.teacherInfo.zsnd,
        dwh: this.recruitUnitList[index].yxm
      }
      this.$api.teacher.teacherRecruitUnitRemoveCheck(params).then(res => {
        if(res.data){
          this.$message.error('导师在该招生单位下存在招生信息，请先进入导师到研究方向页面，删除相应的招生信息')
        }else{
          this.recruitUnitList.splice(index, 1)
        }
      })
    },
    addRecruitUnit () {
      let recruitUnit = {
        ...this.collegeList[this.selectedIndex],
        yxm : this.collegeList[this.selectedIndex].dwh
      }
      let i = 0
      const length = this.recruitUnitList.length
      let canAdd = true
      for(i = 0; i < length; i++){
        if(this.recruitUnitList[i].yxm == recruitUnit.yxm){
          canAdd = false
          break
        }
      }
      if(canAdd){
        this.recruitUnitList.push(recruitUnit)
      }
    },
    handleSave () {
      this.confirmLoading = true
      let params = {
        dsbh: this.teacherInfo.dsbh,
        zsnd: this.teacherInfo.zsnd,
        dwh: this.recruitUnitList.map(unit => {
          return unit.yxm
        }).join(',')
      }
      this.$api.teacher.teacherRecruitUnitSave(params).then(res => {
        this.$message.success(res.msg)
        this.$emit('close')
        this.$emit('refresh')
      }).finally(() => {
        this.confirmLoading = false
      })
    }
  }
}
</script>

<style>

</style>
